<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template #headerSearch-prepend="scope">
        <el-button size="small" icon="el-icon-refresh" type="primary" v-if="hasPagePowerKey('inventory')" @click="trayInventory()">盘点</el-button>
      </template>
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo)">查看</el-button>
        <el-button size="mini" type="text" icon="el-icon-download" @click="doDownload(scope.row.billNo)" style="color: var(--el-color-success);margin-left: 10px;">下载</el-button>
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': 'success'">{{ scope.row.status === 'save'? '保存': '审核' }}</el-tag>
      </template>
    </vb-page>
  </div>
</template>

<script>
import dayjs from "dayjs";

export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    async doDownload(billNo) {
      this.loading = true
      try {
        let res = await this.callApi('download_tray_inventory_list', {billNo: billNo})
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoDetail(billNo = '') {
      let url = '/tray/trayInventoryDetail'
      if (billNo) {
        url += `?billNo=${ billNo }`
      }
      this.$router.push(url)
    },
    async trayInventory() {
      this.loading = true
      try {
        let res = await this.callApi('tray_inventory')
        if (!res.res) {
          return
        }
        this.$message.success('盘点完成!')
        this.$refs.page.reload()
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style scoped>

</style>
